package com.gexiaobao.pigeon.ui.pigeoncatcher

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.model.bean.OrderDrippingDetailResponse
import com.gexiaobao.pigeon.app.util.ImageLoaderManager
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityDriveringOrderDetailBinding
import com.gexiaobao.pigeon.ui.adapter.DrippingOrderDetailPigeonListAdapter
import com.gexiaobao.pigeon.viewmodel.PigeonCatcherViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2024/7/5 14:58
 * @Describe    : 已完成订单（交鸽）
 */
class ActivityFinishDrippingOrder : BaseActivity<PigeonCatcherViewModel, ActivityDriveringOrderDetailBinding>() {

    private val mAdapter: DrippingOrderDetailPigeonListAdapter by lazy { DrippingOrderDetailPigeonListAdapter(arrayListOf()) }

    private var mOrderId = ""
    private var userSignUrl = ""
    private var catcherSignUrl = ""
    private var orgSignUrl = ""

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBtn.llBottomBtn.isVisible = false
        mDatabind.tvChargeBack.isVisible = false
        mOrderId = intent.getIntExtra("id", 0).toString()
        onRefresh()

        mDatabind.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter).addItemDecoration(
            SpaceItemDecoration(0, 0)
        )

        mDatabind.swipeRefreshLayout.init {
            onRefresh()
        }
    }

    fun onRefresh() {
        showLoading(getString(R.string.loading))
        mViewModel.getOrderDidiDetail(mOrderId)
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.ivUserSignImage, mDatabind.ivCatcherSignImage, mDatabind.ivShedSignImage, mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> finish()
                mDatabind.ivUserSignImage -> openBigImage(mDatabind.ivUserSignImage, userSignUrl)
                mDatabind.ivCatcherSignImage -> openBigImage(mDatabind.ivCatcherSignImage, catcherSignUrl)
                mDatabind.ivShedSignImage -> openBigImage(mDatabind.ivShedSignImage, orgSignUrl)
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        /** 获取订单详情 */
        mViewModel.drippingOrderDetailResult.observe(this) {
            if (mDatabind.swipeRefreshLayout.isRefreshing) {
                mDatabind.swipeRefreshLayout.isRefreshing = false
            }
            if (it != null) {
                mDatabind.data = it
                refreshDataUi(it)
            }
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun refreshDataUi(it: OrderDrippingDetailResponse) {
        if (it.list != null && it.list.pigeons.isNotEmpty()) {
            mAdapter.setList(it.list.pigeons)
            mAdapter.notifyDataSetChanged()
        }
        userSignUrl = it.signUserPicUrl
        catcherSignUrl = it.signTransporterPicUrl
        orgSignUrl = it.signOrgPicUrl
        ImageLoaderManager.loadImage(this, userSignUrl, mDatabind.ivUserSignImage)
        ImageLoaderManager.loadImage(this, catcherSignUrl, mDatabind.ivCatcherSignImage)
        ImageLoaderManager.loadImage(this, orgSignUrl, mDatabind.ivShedSignImage)
        mDatabind.llSign.isVisible = true
    }

}